package com.sgb.kjwl.viewmodel.home;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.EnterpriseEditEntity;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

public class HomeCompanyConfigModel extends HBaseViewModel {

    public HomeCompanyConfigModel(@NonNull Application application) {
        super(application);
    }

    //    public MutableLiveData<List<EnterpriseRecEntity>> liveData1 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData2 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData3 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData4 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData5 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData6 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData7 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData8 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData9 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData10 = new MutableLiveData<>();
    public MutableLiveData<List<EnterpriseRecEntity>> liveData11 = new MutableLiveData<>();

    /**
     * 根据企业标题获取类型列表数据
     *
     * @param mItemType 当前企业标题的类型
     * @param isRefresh 当前界面是否处于刷新状态
     */
    public void getCompanyList(String mItemType, boolean isRefresh) {
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        int code = TextUtils.isEmpty(areaCode) ? 0 : Integer.valueOf(areaCode);
        APIManager.getInstance().getBigHomeAPI().getEnterpriseRecList(mItemType, code).enqueue(new ICallBack<BaseEntity<List<EnterpriseRecEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseRecEntity>> data) {
                switch (mItemType) {
                    case "1":
//                        liveData1.postValue(data.getData());
                        break;
                    case "2":
                        liveData2.postValue(data.getData());
                        break;
                    case "3":
                        liveData3.postValue(data.getData());
                        break;
                    case "4":
                        liveData4.postValue(data.getData());
                        break;
                    case "5":
                        liveData5.postValue(data.getData());
                        break;
                }

            }
        });
    }

    /**
     * 根据企业标题获取类型列表数据
     *
     * @param mItemType 当前企业标题的类型
     * @param isRefresh 当前界面是否处于刷新状态
     */
    public void getCompanyConfigList(String mItemType, boolean isRefresh) {
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        int code = TextUtils.isEmpty(areaCode) ? 0 : Integer.valueOf(areaCode);
        APIManager.getInstance().getBigHomeAPI().getEnterpriseRecList(mItemType, code).enqueue(new ICallBack<BaseEntity<List<EnterpriseRecEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseRecEntity>> data) {
                switch (mItemType) {
                    case "1":
                        liveData6.postValue(data.getData());
                        break;
                    case "2":
                        liveData7.postValue(data.getData());
                        break;
                    case "3":
                        liveData8.postValue(data.getData());
                        break;
                    case "4":
                        liveData9.postValue(data.getData());
                        break;
                    case "5":
                        liveData10.postValue(data.getData());
                        break;
                }

            }
        });
    }

    /**
     * 新增-编辑首页推荐列表
     *
     * @param list 当前要缓存或者替换的首页企业菜单
     */
    public void editHomeCompanyMenu(List<EnterpriseRecEntity> list) {
        APIManager.getInstance().getBigHomeAPI().editEnterpriseConfigList(new EnterpriseEditEntity(list)).enqueue(new ICallBack<BaseEntity<List<EnterpriseRecEntity>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseRecEntity>> data) {
                getCompanyConfigList("1", false);
                getCompanyConfigList("4", false);
                getCompanyConfigList("5", false);

                int TYPE = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
                if (TYPE == 1) {
                    getCompanyConfigList("2", false);
                }
                if (TYPE == 0 || TYPE == 1) {
                    getCompanyConfigList("3", false);
                }
            }
        });
    }

    /**
     * 首页企业列表数据
     */
    public void getCompanyList(EnterpriseRecEntity entity) {
        String areaCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
        int code = TextUtils.isEmpty(areaCode) ? 0 : Integer.valueOf(areaCode);
        APIManager.getInstance().getBigHomeAPI().getEnterpriseRecList("1", code).enqueue(new ICallBack<BaseEntity<List<EnterpriseRecEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<EnterpriseRecEntity>> data) {
                if (data.getData().size() >= 6) {
                    MToast.showToast(getApplication(), "首页企业配置已满，请调整首页企业后继续添加");
                } else {
                    List<EnterpriseRecEntity> list = data.getData();
                    list.add(entity);
                    liveData11.postValue(list);
                }
            }
        });
    }
}
